package com.lzh.queue;

import java.util.Scanner;

/**
 * @author liuzhihao
 * @create 2021-10-09 21:16
 * 环形队列的实现
 */
public class CircleQueueDemo {

    public static void main(String[] args) {
        CircleQueue circleQueue = new CircleQueue(5);
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("1.入队(a)");
            System.out.println("2.出队(o)");
            System.out.println("3.显示所有队列(l)");
            System.out.println("4.获取头元素(h)");
            System.out.println("5.当前队列有效数据的个数(s)");
            System.out.println("6.退出(e)");
            System.out.println("请输入你的操作：");
            char key = sc.next().charAt(0);
            switch (key) {
                case 'a':
                    System.out.println("请输入你要添加的元素：");
                    int num = sc.nextInt();
                    circleQueue.addCircleQueue(num);
                    break;
                case 'o':
                    int outCircleQueue = circleQueue.outCircleQueue();
                    System.out.println("出队列的值为：" + outCircleQueue);
                    break;
                case 'l':
                    circleQueue.listCircleQueue();
                    break;
                case 'h':
                    int head = circleQueue.getHead();
                    System.out.println("头元素为：" + head);
                    break;
                case 's':
                    int size = circleQueue.size();
                    System.out.println("当前队列有效数据的个数" + size);
                    break;
                case 'e':
                    loop = false;
                    break;
                default:
                    break;
            }
        }

    }
}

class CircleQueue {

    int font;
    int rear;// rear指向元素的后一个位置
    int[] arrCircleQueue;
    int maxSize;

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        font = 0;
        rear = 0;
        arrCircleQueue = new int[maxSize];
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == font;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return font == rear;
    }

    /**
     * 添加队列
     */
    public void addCircleQueue(int num) {
        // 添加队列之前判断队列是否满
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        // 给队列赋值
        arrCircleQueue[rear] = num;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 出队列
     */
    public int outCircleQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = arrCircleQueue[font];
        font = (font + 1) % maxSize;
        return value;
    }

    /**
     * 获取头元素
     *
     * @return
     */
    public int getHead() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arrCircleQueue[font];
    }

    /**
     * 求出当前队列有效数据的个数
     *
     * @return
     */
    public int size() {
        // rear = 1
        // front = 0
        // maxSize = 3
        return (rear + maxSize - font) % maxSize;
    }

    /**
     * 显示当前队列数据
     */
    public void listCircleQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = font; i < font + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i%maxSize, arrCircleQueue[i % maxSize]);
        }

    }
}


